

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="—— 若发现文章有任何不正确的地方，敬请指出，望不吝赐教，谢谢！@[TOC]  效果展示 在登录窗口进行登录，登录验证成功后跳转到另一个窗口，若失败则弹出提示框 一、准备知识JavaFX 中文官网：点击查看  javaFX 是一个开源的下一代客户端应用平台，适用于基于Java构建的桌面、移动端和嵌入式系统。 它是许多个人和公司的共同努力的成果，目的是为开发丰富的客户端应用提供一个现代、高效、功能">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaFX 实现windows版简易的登录软件 _ 整合 MyBatis 查询数据库账号 _ 窗口切换 _ 使用SceneBuilder进行可视化编辑页面">
<meta property="og:url" content="http://example.com/2022/02/28/JavaFX%20%E5%AE%9E%E7%8E%B0windows%E7%89%88%E7%AE%80%E6%98%93%E7%9A%84%E7%99%BB%E5%BD%95%E8%BD%AF%E4%BB%B6%20_%20%E6%95%B4%E5%90%88%20MyBatis%20%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE%E5%BA%93%E8%B4%A6%E5%8F%B7%20_%20%E7%AA%97%E5%8F%A3%E5%88%87%E6%8D%A2%20_%20%E4%BD%BF%E7%94%A8SceneBuilder%E8%BF%9B%E8%A1%8C%E5%8F%AF%E8%A7%86%E5%8C%96%E7%BC%96%E8%BE%91%E9%A1%B5%E9%9D%A2/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="—— 若发现文章有任何不正确的地方，敬请指出，望不吝赐教，谢谢！@[TOC]  效果展示 在登录窗口进行登录，登录验证成功后跳转到另一个窗口，若失败则弹出提示框 一、准备知识JavaFX 中文官网：点击查看  javaFX 是一个开源的下一代客户端应用平台，适用于基于Java构建的桌面、移动端和嵌入式系统。 它是许多个人和公司的共同努力的成果，目的是为开发丰富的客户端应用提供一个现代、高效、功能">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/d2502921ed4a46a79918cd7f67656f36.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/9a7b553a85c54d6e8f6186c983c29970.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/837240cac5e649ac9b6e7a5fd7ed4d1e.png">
<meta property="article:published_time" content="2022-02-27T16:41:16.000Z">
<meta property="article:modified_time" content="2022-08-22T15:45:23.706Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="JavaFX">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/d2502921ed4a46a79918cd7f67656f36.gif#pic_center">
  
  
  
  <title>JavaFX 实现windows版简易的登录软件 _ 整合 MyBatis 查询数据库账号 _ 窗口切换 _ 使用SceneBuilder进行可视化编辑页面 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="JavaFX 实现windows版简易的登录软件 _ 整合 MyBatis 查询数据库账号 _ 窗口切换 _ 使用SceneBuilder进行可视化编辑页面"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-02-28 00:41" pubdate>
          2022年2月28日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          20k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          168 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">JavaFX 实现windows版简易的登录软件 _ 整合 MyBatis 查询数据库账号 _ 窗口切换 _ 使用SceneBuilder进行可视化编辑页面</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <blockquote>
<p>—— 若发现文章有任何不正确的地方，敬请指出，望不吝赐教，谢谢！<br>@[TOC]</p>
</blockquote>
<h1 id="效果展示"><a href="#效果展示" class="headerlink" title="效果展示"></a>效果展示</h1><hr>
<p>在登录窗口进行登录，登录验证成功后跳转到另一个窗口，若失败则弹出提示框<br><img src="https://img-blog.csdnimg.cn/d2502921ed4a46a79918cd7f67656f36.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="一、准备知识"><a href="#一、准备知识" class="headerlink" title="一、准备知识"></a>一、准备知识</h1><p>JavaFX 中文官网：<a target="_blank" rel="noopener" href="https://openjfx.cn/">点击查看</a></p>
<blockquote>
<p>javaFX 是一个开源的下一代客户端应用平台，适用于基于Java构建的桌面、移动端和嵌入式系统。 它是许多个人和公司的共同努力的成果，目的是为开发丰富的客户端应用提供一个现代、高效、功能齐全的工具包。</p>
</blockquote>
<p>JavaFX架构：<br><img src="https://img-blog.csdnimg.cn/9a7b553a85c54d6e8f6186c983c29970.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="相关工具：Scene-Builder-点击下载"><a href="#相关工具：Scene-Builder-点击下载" class="headerlink" title="相关工具：Scene Builder 点击下载"></a>相关工具：Scene Builder <a target="_blank" rel="noopener" href="https://openjfx.cn/scene-builder/">点击下载</a></h2><blockquote>
<p>Scene Builder 是一款创建美丽的用户界面，并将您的设计变成一个交互式原型。Scene Builder通过创建可直接用于JavaFX应用程序的用户界面，缩小了设计师和开发人员之间的差距。</p>
</blockquote>
<p>JavaFX相关的资料网站：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="http://www.javafxchina.net/main/">JavaFXChina</a> 是国内JavaFX资料最为系统全面的网站之一，他们靠业余时间维护此站点，希望能帮到大家。</p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/837240cac5e649ac9b6e7a5fd7ed4d1e.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="二、运行环境"><a href="#二、运行环境" class="headerlink" title="二、运行环境"></a>二、运行环境</h1><hr>
<ul>
<li>Windows10</li>
<li>MySQL8</li>
<li>JDK8 </li>
<li>Maven3.8.4</li>
<li>IDEA 2021.3.2 Ultimate</li>
<li>其他的版本号请查看引入的Maven依赖</li>
</ul>
<h1 id="三、引入依赖"><a href="#三、引入依赖" class="headerlink" title="三、引入依赖"></a>三、引入依赖</h1><hr>
<p><code>pom.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.uni<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>cs-plugins<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">name</span>&gt;</span>cs-plugins<span class="hljs-tag">&lt;/<span class="hljs-name">name</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">project.build.sourceEncoding</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">junit.version</span>&gt;</span>5.8.1<span class="hljs-tag">&lt;/<span class="hljs-name">junit.version</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">java.version</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">java.version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.openjfx<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>javafx-controls<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;java.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.openjfx<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>javafx-fxml<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;java.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.kordamp.bootstrapfx<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>bootstrapfx-core<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.4.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.junit.jupiter<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit-jupiter-api<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;junit.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.junit.jupiter<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit-jupiter-engine<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;junit.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>mysql<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mysql-connector-java<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>8.0.25<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>	<span class="hljs-comment">&lt;!--日志管理--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>log4j<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>log4j<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.2.17<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">build</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">plugins</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">plugin</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>maven-compiler-plugin<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.8.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">source</span>&gt;</span>$&#123;java.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">source</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">target</span>&gt;</span>$&#123;java.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">target</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">plugin</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">plugin</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.openjfx<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>javafx-maven-plugin<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.0.8<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">executions</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">execution</span>&gt;</span><br>                        <span class="hljs-comment">&lt;!-- Default configuration for running with: mvn clean javafx:run --&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>default-cli<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">mainClass</span>&gt;</span>com.uni/com.uni.App<span class="hljs-tag">&lt;/<span class="hljs-name">mainClass</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">launcher</span>&gt;</span>app<span class="hljs-tag">&lt;/<span class="hljs-name">launcher</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">jlinkZipName</span>&gt;</span>app<span class="hljs-tag">&lt;/<span class="hljs-name">jlinkZipName</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">jlinkImageName</span>&gt;</span>app<span class="hljs-tag">&lt;/<span class="hljs-name">jlinkImageName</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">noManPages</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">noManPages</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">stripDebug</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">stripDebug</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">noHeaderFiles</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">noHeaderFiles</span>&gt;</span><br>                        <span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">execution</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">executions</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">plugin</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">plugins</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">build</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h1 id="四、项目结构介绍"><a href="#四、项目结构介绍" class="headerlink" title="四、项目结构介绍"></a>四、项目结构介绍</h1><hr>
<p>由于工具会涉及数据库的相关交互，同时有视图的概念，这里采用类似于Web的MVC模式进行设计，其中的M表示Model模型，在这里对应于JavaFX的Stage窗口，V则表示View视图，对应于JavaFX的Scene，C表示Controller控制层，JavaFX官方API有提供getController()的方法。</p>
<p>其他的相关介绍：</p>
<table>
<thead>
<tr>
<th align="left">名称</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">controller</td>
<td align="left">存储控制层相关的类，其中比较重要的是Stage相关的类</td>
</tr>
<tr>
<td align="left">dao</td>
<td align="left">存储负责数据库交互的Mapper接口，实现则通过resources文件夹里的XML映射文件</td>
</tr>
<tr>
<td align="left">pojo</td>
<td align="left">存储实体类</td>
</tr>
<tr>
<td align="left">service</td>
<td align="left">存储业务层接口以及对应的Impl实现类</td>
</tr>
<tr>
<td align="left">utils</td>
<td align="left">存储一些工具类，比如获取MyBatis负责数据库交互的mapper类</td>
</tr>
<tr>
<td align="left">App.java</td>
<td align="left">继承于JavaFX的Application类，主要用于启动JavaFX程序以及配置相关的窗口信息</td>
</tr>
<tr>
<td align="left">resources&#x2F;com&#x2F;uni&#x2F;mapper&#x2F;</td>
<td align="left">存储MyBatis里Mapper接口对应的XML映射文件</td>
</tr>
<tr>
<td align="left">login.fxml</td>
<td align="left">使用SceneBuilder工具进行可视化设计的登录的页面</td>
</tr>
<tr>
<td align="left">main.fxml</td>
<td align="left">使用SceneBuilder工具设计的登录成功后的主页面（这里涉及页面切换概念）</td>
</tr>
<tr>
<td align="left">lib</td>
<td align="left">存储一些关键的jar包，可以忽略，Maven导入依赖就行</td>
</tr>
<tr>
<td align="left">database.properties</td>
<td align="left">配置JDBC连接的一些信息，数据库连接地址、驱动类、用户、密码</td>
</tr>
<tr>
<td align="left">mybatis-config.xml</td>
<td align="left">配置项目的MyBatis</td>
</tr>
<tr>
<td align="left">![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/53d488fe705f499cbc9d6cdec25c2051.png#">https://img-blog.csdnimg.cn/53d488fe705f499cbc9d6cdec25c2051.png#</a> &#x3D;50%x)</td>
<td align="left"></td>
</tr>
</tbody></table>
<h1 id="五、配置数据库交互层DAO层"><a href="#五、配置数据库交互层DAO层" class="headerlink" title="五、配置数据库交互层DAO层"></a>五、配置数据库交互层DAO层</h1><hr>
<h2 id="5-1-连接数据库配置"><a href="#5-1-连接数据库配置" class="headerlink" title="5.1 连接数据库配置"></a>5.1 连接数据库配置</h2><p><code>/resources/database.properties</code></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">driver</span>=<span class="hljs-string">com.mysql.cj.jdbc.Driver</span><br><span class="hljs-attr">url</span>=<span class="hljs-string">jdbc:mysql://localhost:3306/数据库名称?useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull</span><br><span class="hljs-attr">user</span>=<span class="hljs-string">数据库用户名</span><br><span class="hljs-attr">password</span>=<span class="hljs-string">数据库密码</span><br></code></pre></td></tr></table></figure>
<p>这里需要注意，为方便代码里获取到配置内容，最好是将该其放在resources资源文件夹里，方便查找以及访问</p>
<p>JDK官方有API操作properties的配置文件，所以不需要手动进行IO流的转换，这里则通过MyBatis框架提供的API直接连接数据库</p>
<p><strong>范例：</strong> 根据MySQL的配置文件创建MyBatis框架的工厂（可通过工厂取Mapper类、执行方法等）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> org.apache.ibatis.io.Resources;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSessionFactoryBuilder;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSessionFactory;<br><br><span class="hljs-type">String</span> <span class="hljs-variable">CONFIG_PATH</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;mybatis-config.xml&quot;</span>;<br><br><span class="hljs-type">InputStream</span> <span class="hljs-variable">config</span> <span class="hljs-operator">=</span> Resources.getResourceAsStream(CONFIG_PATH);<br><br>factory = <span class="hljs-keyword">new</span> <span class="hljs-title class_">SqlSessionFactoryBuilder</span>().build(config);<br></code></pre></td></tr></table></figure>

<h2 id="5-2-MyBatis配置"><a href="#5-2-MyBatis配置" class="headerlink" title="5.2 MyBatis配置"></a>5.2 MyBatis配置</h2><p>在没有SpringBoot下配置MyBatis果然有些复杂了，不过稍微总结一下，也不难理解，它的主要配置内容如下：</p>
<ul>
<li>引入相关的数据库配置文件（可以不引入直接写值，但这会导致耦合度提升，不利于代码修改）</li>
<li>特殊的设置（这里可有可无，而且配置类型比较多，笔者就只改了个日志输出的类logImpl，这样就可以按照自己的日志配置来</li>
<li>设置别名，一是简化实体类（这样可以省略包名看起来简短）二是简化Mapper类的接口类，这样在写XML时指定namespace就可以省略Mapper接口的包名）</li>
<li>配置数据库环境</li>
<li>最后，设置Mapper的XML映射文件地址，这样MyBatis才能找到对应的XML映射</li>
</ul>
<p><code>/resources/mybatis-config.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span> ?&gt;</span><br><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">configuration</span></span><br><span class="hljs-meta">        <span class="hljs-keyword">PUBLIC</span> <span class="hljs-string">&quot;-//mybatis.org//DTD Config 3.0//EN&quot;</span></span><br><span class="hljs-meta">        <span class="hljs-string">&quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;</span>&gt;</span><br><br><span class="hljs-comment">&lt;!-- 通过这个配置文件完成mybatis与数据库的连接 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 引入 database.properties 文件--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span> <span class="hljs-attr">resource</span>=<span class="hljs-string">&quot;database.properties&quot;</span>/&gt;</span><br>    <span class="hljs-comment">&lt;!-- 配置mybatis的log实现为LOG4J --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">settings</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">setting</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;logImpl&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;LOG4J&quot;</span> /&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">settings</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 相关别名配置--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">typeAliases</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--实体类简化--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">typeAlias</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;com.uni.pojo.User&quot;</span> <span class="hljs-attr">alias</span>=<span class="hljs-string">&quot;User&quot;</span>/&gt;</span><br>        <span class="hljs-comment">&lt;!--Mapper接口类的位置--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">package</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;com.uni.mapper&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">typeAliases</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">environments</span> <span class="hljs-attr">default</span>=<span class="hljs-string">&quot;development&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">environment</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;development&quot;</span>&gt;</span><br>            <span class="hljs-comment">&lt;!--配置事务管理，采用JDBC的事务管理  --&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">transactionManager</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;JDBC&quot;</span>/&gt;</span><br>            <span class="hljs-comment">&lt;!-- POOLED:mybatis自带的数据源，JNDI:基于tomcat的数据源 --&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">dataSource</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;POOLED&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;driver&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;driver&#125;&quot;</span>/&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;url&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;url&#125;&quot;</span>/&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;user&#125;&quot;</span>/&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;password&#125;&quot;</span>/&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">dataSource</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">environment</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">environments</span>&gt;</span><br><br><span class="hljs-comment">&lt;!--    Mapper XML映射文件配置--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">mappers</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">mapper</span> <span class="hljs-attr">resource</span>=<span class="hljs-string">&quot;com/uni/mapper/UserMapper.xml&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">mappers</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span><br><br></code></pre></td></tr></table></figure>

<h2 id="5-3-编写MyBatis工具类-DBUtils-java"><a href="#5-3-编写MyBatis工具类-DBUtils-java" class="headerlink" title="5.3 编写MyBatis工具类 DBUtils.java"></a>5.3 编写MyBatis工具类 DBUtils.java</h2><p>关于MyBatis的使用，之前浅学了SSM+SpringBoot，就只记住了@Repository、@Autowired这两个注解，当不使用注解的时候，有多种选择进行创建，这里则使用其中的工厂模式来创建。</p>
<p><code>DBUtils.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.utils;<br><br><span class="hljs-keyword">import</span> org.apache.ibatis.io.Resources;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSession;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSessionFactory;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSessionFactoryBuilder;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><span class="hljs-keyword">import</span> java.io.InputStream;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">DBUtils</span>&#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> <span class="hljs-variable">CONFIG_PATH</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;mybatis-config.xml&quot;</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> SqlSessionFactory factory;<br>    <span class="hljs-comment">// 单例模式</span><br>    <span class="hljs-keyword">static</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">// 获取配置文件输入流</span><br>            <span class="hljs-type">InputStream</span> <span class="hljs-variable">config</span> <span class="hljs-operator">=</span> Resources.getResourceAsStream(CONFIG_PATH);<br>            <span class="hljs-comment">// 创建工厂</span><br>             factory = <span class="hljs-keyword">new</span> <span class="hljs-title class_">SqlSessionFactoryBuilder</span>().build(config);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            e.printStackTrace();<br>        &#125;<br>    &#125;<br>    <span class="hljs-comment">// 获取session会话</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> SqlSession <span class="hljs-title function_">getSession</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> factory.openSession();<br>    &#125;<br>    <span class="hljs-comment">// 关闭session会话</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">close</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-comment">//..</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="5-4-DAO层"><a href="#5-4-DAO层" class="headerlink" title="5.4 DAO层"></a>5.4 DAO层</h2><p>在编写DAO层接口前，需先编写相应的实体类，比如笔者需要实现登录效果，这里就设计一个用户User的Pojo（简单的Java对象) 类</p>
<p><code>User.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.pojo;<br><br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @作者: Unirithe</span><br><span class="hljs-comment"> * @日期: 2022/2/27</span><br><span class="hljs-comment"> **/</span><br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> &#123;<br>    <span class="hljs-keyword">private</span> Integer id;<br>    <span class="hljs-keyword">private</span> String username;<br>    <span class="hljs-keyword">private</span> String password;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">User</span><span class="hljs-params">(Integer id, String username, String password)</span> &#123;<br>        <span class="hljs-built_in">this</span>.id = id;<br>        <span class="hljs-built_in">this</span>.username = username;<br>        <span class="hljs-built_in">this</span>.password = password;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">User</span><span class="hljs-params">(String username, String password)</span> &#123;<br>        <span class="hljs-built_in">this</span>.username = username;<br>        <span class="hljs-built_in">this</span>.password = password;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Integer <span class="hljs-title function_">getId</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> id;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setId</span><span class="hljs-params">(Integer id)</span> &#123;<br>        <span class="hljs-built_in">this</span>.id = id;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getUsername</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> username;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setUsername</span><span class="hljs-params">(String username)</span> &#123;<br>        <span class="hljs-built_in">this</span>.username = username;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getPassword</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> password;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setPassword</span><span class="hljs-params">(String password)</span> &#123;<br>        <span class="hljs-built_in">this</span>.password = password;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;User&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;id=&quot;</span> + id +<br>                <span class="hljs-string">&quot;, username=&#x27;&quot;</span> + username + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&quot;, password=&#x27;&quot;</span> + password + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>Dao层接口类：<code>UserMapper.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.dao;<br><br><span class="hljs-keyword">import</span> com.uni.pojo.User;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @作者: Unirithe</span><br><span class="hljs-comment"> * @日期: 2022/2/27</span><br><span class="hljs-comment"> **/</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">UserMapper</span> &#123;<br>    User <span class="hljs-title function_">selectByPassword</span><span class="hljs-params">(User user)</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>Dao层XML映射文件</strong>：<code>UserMapper.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">mapper</span> <span class="hljs-keyword">PUBLIC</span> <span class="hljs-string">&quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;</span> <span class="hljs-string">&quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">mapper</span> <span class="hljs-attr">namespace</span>=<span class="hljs-string">&quot;com.uni.dao.UserMapper&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;selectByPassword&quot;</span> <span class="hljs-attr">parameterType</span>=<span class="hljs-string">&quot;User&quot;</span> <span class="hljs-attr">resultType</span>=<span class="hljs-string">&quot;User&quot;</span>&gt;</span><br>        SELECT * FROM cs_user<br>        WHERE username = #&#123;username&#125; AND password = #&#123;password&#125;<br>    <span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">mapper</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h1 id="六、编写Service业务逻辑层"><a href="#六、编写Service业务逻辑层" class="headerlink" title="六、编写Service业务逻辑层"></a>六、编写Service业务逻辑层</h1><hr>
<p>接口类：<code>UserService.java</code> （实现类放在最后的部分，因为它用到了JavaFX的相关的一些类）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.service;<br><br><span class="hljs-keyword">import</span> com.uni.pojo.User;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @作者: Unirithe</span><br><span class="hljs-comment"> * @日期: 2022/2/27</span><br><span class="hljs-comment"> **/</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">UserService</span> &#123;<br>    <span class="hljs-keyword">public</span> User <span class="hljs-title function_">login</span><span class="hljs-params">(User user)</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="七、JavaFX相关的类"><a href="#七、JavaFX相关的类" class="headerlink" title="七、JavaFX相关的类"></a>七、JavaFX相关的类</h1><hr>
<h2 id="7-1-启动类"><a href="#7-1-启动类" class="headerlink" title="7.1 启动类"></a>7.1 启动类</h2><p><code>App.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni;<br><br><span class="hljs-keyword">import</span> com.uni.controller.StageController;<br><span class="hljs-keyword">import</span> javafx.application.Application;<br><span class="hljs-keyword">import</span> javafx.stage.Stage;<br><br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Application</span> &#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">String</span> <span class="hljs-variable">mainViewID</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;MainView&quot;</span>;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">String</span> <span class="hljs-variable">mainViewRes</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;main.fxml&quot;</span>;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">String</span> <span class="hljs-variable">loginViewID</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;LoginView&quot;</span>;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">String</span> <span class="hljs-variable">loginViewRes</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;login.fxml&quot;</span>;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">start</span><span class="hljs-params">(Stage primaryStage)</span> &#123;<br>        <span class="hljs-comment">//新建一个StageController控制器</span><br>        <span class="hljs-type">StageController</span> <span class="hljs-variable">stageController</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StageController</span>();<br>        <span class="hljs-comment">//设置默认的主窗口</span><br>        <span class="hljs-comment">//加载两个窗口</span><br>        stageController.loadStage(loginViewID, loginViewRes);<br>        stageController.loadStage(mainViewID, mainViewRes);<br>        <span class="hljs-comment">//切换主窗口</span><br>        stageController.change(loginViewID);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        launch();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="7-2-编写StageController窗口控制类"><a href="#7-2-编写StageController窗口控制类" class="headerlink" title="7.2 编写StageController窗口控制类"></a>7.2 编写StageController窗口控制类</h2><p>该类使用HashMap结构来存储对应的窗口名称、对应的Stage窗口对象，使用双端队列进行窗口的切换控制，目前还存在一些不足，仅作参考，相关的方法以及介绍。</p>
<table>
<thead>
<tr>
<th align="left">方法名</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">void addStage(String a, Stage b)</td>
<td align="left">根据名称添加Stage窗口到Map结构中</td>
</tr>
<tr>
<td align="left">Stage getStage(String a )</td>
<td align="left">根据Stage窗口名称获取对应的Stage对象</td>
</tr>
<tr>
<td align="left">change(String a)</td>
<td align="left">根据Stage名称切换到对应的窗口（要求Stage存在于Map中）设计双端队列的处理</td>
</tr>
<tr>
<td align="left">loadStage(String a, String b, StageStyle …)</td>
<td align="left">根据Stage的名称和对应的fxml文件位置，加上可选的窗口风格加载新的Stage对象，加载完毕后会保存到Map结构</td>
</tr>
<tr>
<td align="left">unloadStage(String a)</td>
<td align="left">根据Stage名称删除相应的窗口</td>
</tr>
<tr>
<td align="left">stages</td>
<td align="left">静态变量，存储所有的Stage名称以及Stage信息</td>
</tr>
<tr>
<td align="left">queue</td>
<td align="left">静态变量， 存储当前的窗口序列，可用于返回操作（这里暂未使用）</td>
</tr>
</tbody></table>
<p><code>StageController.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.controller;<br><br><span class="hljs-keyword">import</span> com.uni.App;<br><span class="hljs-keyword">import</span> javafx.fxml.FXMLLoader;<br><span class="hljs-keyword">import</span> javafx.scene.Scene;<br><span class="hljs-keyword">import</span> javafx.scene.layout.Pane;<br><span class="hljs-keyword">import</span> javafx.stage.Stage;<br><span class="hljs-keyword">import</span> javafx.stage.StageStyle;<br><br><span class="hljs-keyword">import</span> java.util.Deque;<br><span class="hljs-keyword">import</span> java.util.HashMap;<br><span class="hljs-keyword">import</span> java.util.LinkedList;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @作者: Unirithe</span><br><span class="hljs-comment"> * @日期: 2022/2/27</span><br><span class="hljs-comment"> **/</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">StageController</span> &#123;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> HashMap&lt;String, Stage&gt; stages = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;String, Stage&gt;();       <span class="hljs-comment">// 存储所有Stage</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Deque&lt;Stage&gt; queue = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;&gt;();                            <span class="hljs-comment">// 窗口双端队列（先进后出）（切换窗口有效）</span><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 添加Stage，保存到Map结构里</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> name 窗口名称</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> stage 窗口的stage，由该类的&#123;<span class="hljs-doctag">@link</span> #loadStage&#125;方法进行创建</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">addStage</span><span class="hljs-params">(String name, Stage stage)</span> &#123;<br>        stages.put(name, stage);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 根据Stage名称从Map结构里获取相应的Stage</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> name Stage的对应名称，加载通过&#123;<span class="hljs-doctag">@link</span> #loadStage(String, String, StageStyle...)&#125; 添加通过&#123;<span class="hljs-doctag">@link</span> #addStage(String, Stage)&#125;</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> Map里存在的Stage对象</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> Stage <span class="hljs-title function_">getStage</span><span class="hljs-params">(String name)</span> &#123;<br>        <span class="hljs-keyword">return</span> stages.get(name);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 切换Map结构里存在的Stage，将其压入栈中。若不存在则抛出异常</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> stageName 切换的Stage名称</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">change</span><span class="hljs-params">(String stageName)</span> &#123;<br>        <span class="hljs-comment">// 首先获取当前的state</span><br>        <span class="hljs-type">Stage</span> <span class="hljs-variable">stage</span> <span class="hljs-operator">=</span> stages.get(stageName);<br>        <span class="hljs-keyword">if</span>(stage == <span class="hljs-literal">null</span>)<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">NullPointerException</span>(<span class="hljs-string">&quot;未获取到指定的Stage，请确保它已经使用过addStage()/loadStage()&quot;</span>);<br>        <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-comment">// 遍历队列</span><br>            <span class="hljs-keyword">if</span> (queue != <span class="hljs-literal">null</span> &amp;&amp; !queue.isEmpty()) &#123;<br>                <span class="hljs-comment">// 将队首的传移动到队尾</span><br>                queue.push(queue.pollFirst());<br>                <span class="hljs-comment">// 关闭所有窗口</span><br>                <span class="hljs-keyword">for</span> (Stage temp : queue) &#123;<br>                    <span class="hljs-comment">// 若当前窗口是要切换的窗口，则移除（防止重复）</span><br>                    <span class="hljs-keyword">if</span>(temp.equals(stage))<br>                        queue.remove(temp);<br>                    temp.close();<br>                &#125;<br>            &#125;<br>            <span class="hljs-comment">// 头插法</span><br>            queue.addFirst(stage);<br>            <span class="hljs-comment">// 启动队首窗口</span><br>            queue.getFirst().show();<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 加载Stage资源，同时会调用&#123;<span class="hljs-doctag">@link</span> #addStage(String, Stage)&#125;，最终存储到 Map结构里</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> name Stage的名称</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> resources Stage的资源位置（建议在resources资源文件夹下）</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> styles  Stage的Style风格，可指定多个风格</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 返回是否加载成功，只要不报错则说明加载以及保存成功, 成功返回true，反之返回false</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">loadStage</span><span class="hljs-params">(String name, String resources, StageStyle... styles)</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">//加载FXML资源文件</span><br>            <span class="hljs-type">FXMLLoader</span> <span class="hljs-variable">loader</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">FXMLLoader</span>(App.class.getResource(resources));<br>            <span class="hljs-type">Pane</span> <span class="hljs-variable">tempPane</span> <span class="hljs-operator">=</span> loader.load();<br>            <span class="hljs-comment">//通过Loader获取FXML对应的ViewCtr，并将本StageController注入到ViewCtr中</span><br><br>            <span class="hljs-comment">// 通过官方API获取当前Stage的Controller,利用官方提供的泛型设置将其返回为接口ControlledStage</span><br>            <span class="hljs-type">ControlledStage</span> <span class="hljs-variable">controlledStage</span> <span class="hljs-operator">=</span> loader.getController();<br>            <span class="hljs-comment">// 调用相应的set方法，为当前Staged设置现在的Controller列</span><br>            controlledStage.setStageController(<span class="hljs-built_in">this</span>);<br><br>            <span class="hljs-comment">//构造对应的Stage</span><br>            <span class="hljs-type">Scene</span> <span class="hljs-variable">tempScene</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Scene</span>(tempPane);<br>            <span class="hljs-type">Stage</span> <span class="hljs-variable">tempStage</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stage</span>();<br>            tempStage.setScene(tempScene);<br><br>            <span class="hljs-comment">//初始化窗口的风格</span><br>            <span class="hljs-keyword">for</span> (StageStyle style : styles) &#123;<br>                tempStage.initStyle(style);<br>            &#125;<br>            <span class="hljs-comment">//将设置好Stage添加到Map结构</span><br>            <span class="hljs-built_in">this</span>.addStage(name, tempStage);<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>            e.printStackTrace();<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/***</span><br><span class="hljs-comment">     * 卸载指定的Stage</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> name Stage的名称，这Stage在通过&#123;<span class="hljs-doctag">@link</span> #addStage&#125; 或者 &#123;<span class="hljs-doctag">@link</span> #loadStage&#125;两种方法添加后的菜菜有效</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 是否卸载成功，成功为true，反之为false</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">unloadStage</span><span class="hljs-params">(String name)</span> &#123;<br>        <span class="hljs-keyword">if</span> (stages.remove(name) == <span class="hljs-literal">null</span>) &#123;<br>            System.out.println(<span class="hljs-string">&quot;窗口不存在，请检查名称&quot;</span>);<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            System.out.println(<span class="hljs-string">&quot;窗口移除成功&quot;</span>);<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        &#125;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>
<h2 id="7-3-公用接口ControlledStage"><a href="#7-3-公用接口ControlledStage" class="headerlink" title="7.3 公用接口ControlledStage"></a>7.3 公用接口ControlledStage</h2><p>该接口的作用是要求实现setStageController()方法，StageController则是上面那个自定义的用于处理JavaFX窗口显示的控制层类，当我们绑定一些按钮，要实现界面跳转时就得通过Controller层的逻辑代码进行处理。所以我们规定其他的Controller类都需要实现这个接口，这提示该类需要有一个StageController对象作为成员变量，方便进行窗口切换的操作。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.controller;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @作者: Unirithe</span><br><span class="hljs-comment"> * @日期: 2022/2/27</span><br><span class="hljs-comment"> **/</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">ControlledStage</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setStageController</span><span class="hljs-params">(StageController stageController)</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="7-4-组件的Controller类"><a href="#7-4-组件的Controller类" class="headerlink" title="7.4 组件的Controller类"></a>7.4 组件的Controller类</h2><h3 id="7-4-1-登录控制层"><a href="#7-4-1-登录控制层" class="headerlink" title="7.4.1 登录控制层"></a>7.4.1 登录控制层</h3><p>相关的fxml，通过SceneBuilder可视化工具进行设计</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><br><span class="hljs-meta">&lt;?import javafx.scene.control.Button?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.control.CheckBox?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.control.Label?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.control.Menu?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.control.MenuBar?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.control.MenuItem?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.control.PasswordField?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.control.TextField?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.layout.AnchorPane?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.layout.VBox?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.text.Font?&gt;</span><br><span class="hljs-meta">&lt;?import javafx.scene.text.Text?&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">VBox</span> <span class="hljs-attr">prefHeight</span>=<span class="hljs-string">&quot;407.0&quot;</span> <span class="hljs-attr">prefWidth</span>=<span class="hljs-string">&quot;560.0&quot;</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://javafx.com/javafx/8.0.171&quot;</span> <span class="hljs-attr">xmlns:fx</span>=<span class="hljs-string">&quot;http://javafx.com/fxml/1&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">fx:controller</span>=<span class="hljs-string">&quot;com.uni.controller.LoginController&quot;</span></span><br><span class="hljs-tag">&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">children</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">MenuBar</span> <span class="hljs-attr">VBox.vgrow</span>=<span class="hljs-string">&quot;NEVER&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">menus</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">Menu</span> <span class="hljs-attr">mnemonicParsing</span>=<span class="hljs-string">&quot;false&quot;</span> <span class="hljs-attr">text</span>=<span class="hljs-string">&quot;帮助&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">items</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span> <span class="hljs-attr">mnemonicParsing</span>=<span class="hljs-string">&quot;false&quot;</span> <span class="hljs-attr">text</span>=<span class="hljs-string">&quot;About MyHelloApp&quot;</span> /&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">items</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">Menu</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">menus</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">MenuBar</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">AnchorPane</span> <span class="hljs-attr">maxHeight</span>=<span class="hljs-string">&quot;-1.0&quot;</span> <span class="hljs-attr">maxWidth</span>=<span class="hljs-string">&quot;-1.0&quot;</span> <span class="hljs-attr">prefHeight</span>=<span class="hljs-string">&quot;321.0&quot;</span> <span class="hljs-attr">prefWidth</span>=<span class="hljs-string">&quot;520.0&quot;</span> <span class="hljs-attr">VBox.vgrow</span>=<span class="hljs-string">&quot;ALWAYS&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">children</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">TextField</span> <span class="hljs-attr">fx:id</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">layoutX</span>=<span class="hljs-string">&quot;217.0&quot;</span> <span class="hljs-attr">layoutY</span>=<span class="hljs-string">&quot;119.0&quot;</span> <span class="hljs-attr">prefHeight</span>=<span class="hljs-string">&quot;35.0&quot;</span> <span class="hljs-attr">prefWidth</span>=<span class="hljs-string">&quot;202.0&quot;</span> /&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">PasswordField</span> <span class="hljs-attr">fx:id</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">layoutX</span>=<span class="hljs-string">&quot;217.0&quot;</span> <span class="hljs-attr">layoutY</span>=<span class="hljs-string">&quot;175.0&quot;</span> <span class="hljs-attr">prefHeight</span>=<span class="hljs-string">&quot;35.0&quot;</span> <span class="hljs-attr">prefWidth</span>=<span class="hljs-string">&quot;202.0&quot;</span> /&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">fx:id</span>=<span class="hljs-string">&quot;btLogin&quot;</span> <span class="hljs-attr">onAction</span>=<span class="hljs-string">&quot;#btLogin&quot;</span> <span class="hljs-attr">defaultButton</span>=<span class="hljs-string">&quot;true&quot;</span> <span class="hljs-attr">layoutX</span>=<span class="hljs-string">&quot;207.0&quot;</span> <span class="hljs-attr">layoutY</span>=<span class="hljs-string">&quot;253.0&quot;</span> <span class="hljs-attr">mnemonicParsing</span>=<span class="hljs-string">&quot;false&quot;</span> <span class="hljs-attr">prefHeight</span>=<span class="hljs-string">&quot;30.0&quot;</span> <span class="hljs-attr">prefWidth</span>=<span class="hljs-string">&quot;195.0&quot;</span> <span class="hljs-attr">text</span>=<span class="hljs-string">&quot;登录&quot;</span> /&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">layoutX</span>=<span class="hljs-string">&quot;132.0&quot;</span> <span class="hljs-attr">layoutY</span>=<span class="hljs-string">&quot;253.0&quot;</span> <span class="hljs-attr">mnemonicParsing</span>=<span class="hljs-string">&quot;false&quot;</span> <span class="hljs-attr">text</span>=<span class="hljs-string">&quot;注册&quot;</span> /&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">CheckBox</span> <span class="hljs-attr">layoutX</span>=<span class="hljs-string">&quot;331.0&quot;</span> <span class="hljs-attr">layoutY</span>=<span class="hljs-string">&quot;219.0&quot;</span> <span class="hljs-attr">mnemonicParsing</span>=<span class="hljs-string">&quot;false&quot;</span> <span class="hljs-attr">text</span>=<span class="hljs-string">&quot;记住密码&quot;</span> /&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">Text</span> <span class="hljs-attr">layoutX</span>=<span class="hljs-string">&quot;45.0&quot;</span> <span class="hljs-attr">layoutY</span>=<span class="hljs-string">&quot;82.0&quot;</span> <span class="hljs-attr">strokeType</span>=<span class="hljs-string">&quot;OUTSIDE&quot;</span> <span class="hljs-attr">strokeWidth</span>=<span class="hljs-string">&quot;0.0&quot;</span> <span class="hljs-attr">text</span>=<span class="hljs-string">&quot;CS1.6插件管理系统-登录&quot;</span>&gt;</span><br>               <span class="hljs-tag">&lt;<span class="hljs-name">font</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">Font</span> <span class="hljs-attr">size</span>=<span class="hljs-string">&quot;41.0&quot;</span> /&gt;</span><br>               <span class="hljs-tag">&lt;/<span class="hljs-name">font</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">Label</span> <span class="hljs-attr">layoutX</span>=<span class="hljs-string">&quot;132.0&quot;</span> <span class="hljs-attr">layoutY</span>=<span class="hljs-string">&quot;120.0&quot;</span> <span class="hljs-attr">text</span>=<span class="hljs-string">&quot;账号&quot;</span>&gt;</span><br>               <span class="hljs-tag">&lt;<span class="hljs-name">font</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">Font</span> <span class="hljs-attr">size</span>=<span class="hljs-string">&quot;25.0&quot;</span> /&gt;</span><br>               <span class="hljs-tag">&lt;/<span class="hljs-name">font</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">Label</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">Label</span> <span class="hljs-attr">layoutX</span>=<span class="hljs-string">&quot;132.0&quot;</span> <span class="hljs-attr">layoutY</span>=<span class="hljs-string">&quot;176.0&quot;</span> <span class="hljs-attr">text</span>=<span class="hljs-string">&quot;密码&quot;</span>&gt;</span><br>               <span class="hljs-tag">&lt;<span class="hljs-name">font</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">Font</span> <span class="hljs-attr">size</span>=<span class="hljs-string">&quot;25.0&quot;</span> /&gt;</span><br>               <span class="hljs-tag">&lt;/<span class="hljs-name">font</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">Label</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">children</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">AnchorPane</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">children</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">VBox</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>在登录控制层中，通过@FXML获取到了fxml格式的UI配置文件里使用<code>fx:id</code>标记的组件，然后直接调用业务层的login方法进行登录，最后根据返回的结果是否为null则判断是否登录成功，若登录成功则需要跳转窗口，若失败则弹出提示窗口（这个功能在业务逻辑层实现）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.controller;<br><br><span class="hljs-keyword">import</span> com.uni.App;<br><span class="hljs-keyword">import</span> com.uni.pojo.User;<br><span class="hljs-keyword">import</span> com.uni.service.UserService;<br><span class="hljs-keyword">import</span> com.uni.utils.ServiceUtils;<br><span class="hljs-keyword">import</span> javafx.event.ActionEvent;<br><span class="hljs-keyword">import</span> javafx.fxml.FXML;<br><span class="hljs-keyword">import</span> javafx.fxml.Initializable;<br><span class="hljs-keyword">import</span> javafx.scene.control.PasswordField;<br><span class="hljs-keyword">import</span> javafx.scene.control.TextField;<br><br><span class="hljs-keyword">import</span> java.net.URL;<br><span class="hljs-keyword">import</span> java.util.ResourceBundle;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">LoginController</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">ControlledStage</span>, Initializable &#123;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">UserService</span> <span class="hljs-variable">userService</span> <span class="hljs-operator">=</span> ServiceUtils.userService;<br><br>    StageController controller;<br><br>    <span class="hljs-meta">@FXML</span><br>    <span class="hljs-keyword">private</span> TextField username;<br><br>    <span class="hljs-meta">@FXML</span><br>    <span class="hljs-keyword">private</span> PasswordField password;<br><br>    <span class="hljs-meta">@FXML</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">btLogin</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-type">User</span> <span class="hljs-variable">login</span> <span class="hljs-operator">=</span> userService.login(<span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>(username.getText(), password.getText()));<br>        <span class="hljs-keyword">if</span>(login != <span class="hljs-literal">null</span>)<br>            goToMain();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setStageController</span><span class="hljs-params">(StageController stageController)</span> &#123;<br>        <span class="hljs-built_in">this</span>.controller = stageController;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">initialize</span><span class="hljs-params">(URL location, ResourceBundle resources)</span> &#123;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">goToMain</span><span class="hljs-params">()</span>&#123;<br>        controller.change(App.mainViewID);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="7-4-2-主页面控制层"><a href="#7-4-2-主页面控制层" class="headerlink" title="7.4.2 主页面控制层"></a>7.4.2 主页面控制层</h3><p>这里还没有设计逻辑跳转，但可以参照之前的登录控制层进行添加</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.controller;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @作者: Unirithe</span><br><span class="hljs-comment"> * @日期: 2022/2/27</span><br><span class="hljs-comment"> **/</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainController</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">ControlledStage</span>&#123;<br>    <span class="hljs-keyword">private</span> StageController controller;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setStageController</span><span class="hljs-params">(StageController stageController)</span> &#123;<br>        <span class="hljs-built_in">this</span>.controller = stageController;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="八、核心业务逻辑"><a href="#八、核心业务逻辑" class="headerlink" title="八、核心业务逻辑"></a>八、核心业务逻辑</h1><p>之前声明了UserService接口，定义了login()方法，先进行补充：</p>
<p>这里用到了JavaFX的Alert警告框，通过调用showAndWati()方法就可以弹出这个窗口，则密码验证失败后弹出提示用户输入错误，方便用户操作。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.service.impl;<br><br><span class="hljs-keyword">import</span> com.uni.dao.UserMapper;<br><span class="hljs-keyword">import</span> com.uni.pojo.User;<br><span class="hljs-keyword">import</span> com.uni.service.UserService;<br><span class="hljs-keyword">import</span> com.uni.utils.DBUtils;<br><span class="hljs-keyword">import</span> javafx.scene.control.Alert;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @作者: Unirithe</span><br><span class="hljs-comment"> * @日期: 2022/2/27</span><br><span class="hljs-comment"> **/</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UserService</span> &#123;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> UserMapper userMapper;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">UserServiceImpl</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">if</span>(userMapper == <span class="hljs-literal">null</span>)<br>            userMapper = DBUtils.getSession().getMapper(UserMapper.class);<br>    &#125;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> User <span class="hljs-title function_">login</span><span class="hljs-params">(User user)</span> &#123;<br>        <span class="hljs-type">User</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> userMapper.selectByPassword(user) ;<br>        <span class="hljs-keyword">if</span>(result == <span class="hljs-literal">null</span>)&#123;<br>            <span class="hljs-type">Alert</span> <span class="hljs-variable">alert</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Alert</span>(Alert.AlertType.WARNING);<br>            alert.setTitle(<span class="hljs-string">&quot;登录提示&quot;</span>);<br>            alert.setContentText(<span class="hljs-string">&quot;输入的账号或密码有误&quot;</span>);<br>            alert.showAndWait();<br>        &#125;<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/JavaSE/" class="category-chain-item">JavaSE</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/JavaFX/">#JavaFX</a>
      
        <a href="/tags/Java/">#Java</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>JavaFX 实现windows版简易的登录软件 _ 整合 MyBatis 查询数据库账号 _ 窗口切换 _ 使用SceneBuilder进行可视化编辑页面</div>
      <div>http://example.com/2022/02/28/JavaFX 实现windows版简易的登录软件 _ 整合 MyBatis 查询数据库账号 _ 窗口切换 _ 使用SceneBuilder进行可视化编辑页面/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年2月28日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/03/01/%E6%9C%88%E5%BA%A6%E6%80%BB%E7%BB%93%20_%202022%E5%B9%B402%E6%9C%88%20_%20Web%E5%88%9D%E6%AC%A1%E4%BD%BF%E7%94%A8SpringBoot%E5%BC%80%E5%8F%91%E5%BF%83%E5%BE%97%20_%20%E5%81%87%E6%9C%9F%E4%B8%8E%E5%A4%A7%E4%B8%89%E4%B8%8B%E6%96%B0%E5%AD%A6%E6%9C%9F/" title="月度总结 _ 2022年02月 _ Web初次使用SpringBoot开发心得 _ 假期与大三下新学期">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">月度总结 _ 2022年02月 _ Web初次使用SpringBoot开发心得 _ 假期与大三下新学期</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/02/23/BUG%E5%B7%B2%E8%A7%A3%E5%86%B3%20_%20%E4%BD%BF%E7%94%A8JQuery%E7%9A%84serialize%E5%BA%8F%E5%88%97%E5%8C%96%E8%A1%A8%E5%8D%95%E6%97%B6%EF%BC%8C%E6%97%A0%E6%B3%95%E8%8E%B7%E5%8F%96%E5%88%B0%E8%A1%A8%E5%8D%95%E5%86%85%E6%9C%AA%E9%80%89%E4%B8%AD%E5%8D%95_%E5%A4%9A%E9%80%89%E6%A1%86%E7%9A%84%E5%86%85%E5%AE%B9%20_%20%E6%89%8B%E5%8A%A8%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E5%8A%A0%E5%BC%BA%E7%89%88%E7%9A%84serialize%E6%96%B9%E6%B3%95%E8%8E%B7%E5%8F%96/" title="BUG已解决 _ 使用JQuery的serialize序列化表单时，无法获取到表单内未选中单_多选框的内容 _ 手动编写一个加强版的serialize方法获取">
                        <span class="hidden-mobile">BUG已解决 _ 使用JQuery的serialize序列化表单时，无法获取到表单内未选中单_多选框的内容 _ 手动编写一个加强版的serialize方法获取</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
